// Copyright 2023 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.android.cloudapp;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;

public class CloudAppService extends Service {
    private static final String TAG = "CloudAppService";
    private CloudAppCommunication mCommunication = null;

    public CloudAppService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        CloudAppLog.i(TAG,"onBind.");
        if (mCommunication == null) {
            mCommunication = new CloudAppCommunication();
        }
        return mCommunication;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        CloudAppLog.i(TAG,"onUnbind.");
        mCommunication = null;
        return super.onUnbind(intent);
    }

    @Override
    public void onRebind(Intent intent) {
        CloudAppLog.i(TAG, "onRebind.");
        super.onRebind(intent);
    }

    @Override
    public void onCreate() {
        CloudAppLog.i(TAG, "onCreate.");
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        CloudAppLog.i(TAG, "onDestroy.");
        super.onDestroy();
        mCommunication = null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        CloudAppLog.i(TAG, "onStartCommand.");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channel_id = "CloudAppService-id";
            String channel_name = "CloudAppService-name";
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel(channel_id, channel_name, NotificationManager.IMPORTANCE_HIGH);
            if (manager != null) {
                manager.createNotificationChannel(channel);
            }
            Notification notification = new Notification.Builder(this).setChannelId(channel_id).build();
            startForeground(100, notification);
        }
        return START_STICKY;
    }
}